import axios from 'axios';
import { loadAppURL, is_weixin, weChatHideMenu } from './common.js';
import deletedImage from '../images/report.deleted.png';
import vipImage from '../images/report.deleted.png';

axios.defaults.baseURL = 'http://192.168.0.111/bdzg-api/api/';
axios.defaults.headers['deviceType'] = 3;

axios.interceptors.response.use((response) => {
    try {
        if(response.data.code === 200) {
            return response.data.data || true;
        } else if(response.data.code === 503) {
            //503错误调用APP方法
            loadAppURL('bdzg://set_error_code?errorCode=503');
            if(is_weixin()) {
                weChatHideMenu();
            }
        } else if(response.data.code === 506) {
            //数据不存在
            document.body.innerHTML = `<img style="width:100%;" src="${deletedImage}"/>`
            if(is_weixin()) {
                weChatHideMenu();
            }
        } else if(response.data.code === 106) {
            //会员过期
            document.body.innerHTML = `<img style="width:100%;" src="${vipImage}"/>`
            if(is_weixin()) {
                weChatHideMenu();
            }
        }        
    } catch (error) {
        
    }
    return false;
}, (error) => {
    return Promise.reject(error);
})

export default axios;